<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
</head>
<body>
        <div id="app">
            <router-link :to="{name:'home'}">home</router-link>
            <router-link to="/about">关于我们</router-link>
            <router-link to="/login">登陆</router-link>
            <router-link to="/usercenter">账号中心</router-link>
            <router-link to="/usercenter/pwd">密码管理</router-link>
            <button @click="dc">登出</button>
            <router-view></router-view>
        </div>
 
        <template id="logintpl">
                <div>
                    <button @click="d2a">登陆</button>
                   
                </div>
         </template>
          <script>
         
            const routes = [
                        {
                            name:'home',
                            path:'/',
                            component:{
                                template:'<div> shouye neirong</div>'
                            }
                        },
                        {
                            
                            path:'/about',
                            meta:{
                                requireLogin:true
                            },
                            component:{
                                template:'<div>关于我们</div>'
                            }
                        },
                        {
                            path:'/usercenter',
                            meta:{
                                requireLogin:true
                            },
                            component:{
                                template:"<div>个人用户中心<router-view></router-view></div>",
                               
                            } ,
                            children:[
                                    {
                                        path:'pwd',
                                        meta:{
                                                requireLogin:true
                                            },
                                        component:{
                                        template:'<div>密码管理</div>'}
                                    }
                                ]
                        },
                        {
                            path:'/login',
                            component:{
                                template:"#logintpl",
                           
                                methods:{
                                    d2a:function(){
                                     isLogin=false;
                                    
                                         },
                 
                                } 
                            },
                        }



                       ]
            const router=new VueRouter({
                routes
            }); 
             var isLogin

             
            router.beforeEach((to,from,next)=>{
              
                console.log(isLogin)
                if(to.meta.requireLogin&&isLogin){
                    next("/login")
                }else{
                    next()
                }
              
            }
            )
            new Vue({
                el:'#app',
                methods:{

                   dc:function(){
                    isLogin=true;
                   
                   },
                 
                },
                router
            });
          </script>
</body>
</html>